<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'小程序列表'),
	array('op'=>'set','title'=>'直播参数设置'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['wxapp_id']){
		$params['wxapp_id']=$_RQ['wxapp_id'];
		$return['params']['wxapp_id']=$_RQ['wxapp_id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_wxapp_live',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$wxapp = pdo_get('core_account_wxapp',array('id'=>$item['wxapp_id']),array('title','plugin_name'));
			$wxapp['plugin_title']=pdo_getcolumn('core_plugins',array('name'=>$wxapp['plugin_name']),'title');
			$item=array_merge($wxapp,$item);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list',
			'tip'=>'直播间是根据小程序来获取的，要先去微信小程序后台https://mp.weixin.qq.com，开通“直播”功能，并添加直播间，设置直播抽奖等活动！',
			'edit'=>false,'list'=>array(
			array('type'=>'string','title'=>'小程序id','name'=>'wxapp_id'),
			array('type'=>'string','title'=>'小程序名称','name'=>'title'),
			array('type'=>'string','title'=>'绑定的应用','name'=>'plugin_title'),
		),'action'=>array(
			array('action'=>'jy_weishop/activity.live.rooms/','title'=>'直播间管理','type'=>'href'),
		)),
		'search_one'=>array('name'=>'wxapp_id','placeholder'=>'输入小程序id'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='rooms'){
	$live_id=intval($_RQ['id']);
	$params=array('uniacid'=>$_SESSION['uniacid'],'live_id'=>$live_id);
	if($_RQ['goodsid']){
		$params['goodsid']=$_RQ['goodsid'];
		$return['params']['goodsid']=$_RQ['goodsid'];
	}
	if($_RQ['roomsid']){
		$params['roomsid']=$_RQ['roomsid'];
		$return['params']['roomsid']=$_RQ['roomsid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$list=pdo_getslice('core_account_wxapp_live_rooms',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$goods = pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']),array('title','image'));
			$item=array_merge($goods,$item);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list',
			'tip'=>'本直播间是根据小程序来获取的，要先去微信小程序后台https://mp.weixin.qq.com，开通“直播”功能，并添加直播间，设置直播抽奖等活动！',
			'edit'=>false,'list'=>array(
			array('type'=>'string','title'=>'直播间ID','name'=>'roomsid'),
			array('type'=>'string','title'=>'直播间名称','name'=>'name'),
			array('type'=>'string','title'=>'主播','name'=>'anchor_name'),
			array('type'=>'string','title'=>'绑定的商品ID','name'=>'goodsid'),
			array('type'=>'image','title'=>'直播间图片','name'=>'cover_img'),
			array('type'=>'time','title'=>'开始时间','name'=>'start_time'),
			array('type'=>'time','title'=>'结束时间','name'=>'end_time'),
			array('type'=>'switch','title'=>'审核状态','name'=>'ischeck','options'=>'ischeck'),
			
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_weishop/activity.live.get_data/get_rooms/live_id='.$live_id,'title'=>'一键同步直播间'),
		),'action'=>array(
			array('op'=>'setgoods','title'=>'选择商品','type'=>'modal'),
		)),
		array('type'=>'choose','width'=>18,'action'=>'jy_weishop/goods.get_goods/','name'=>'goodsid','title'=>'选择商品'),
	);
	$return['htmls']=$htmls;
	$return['ischeck']=array('未审核','已审核');
	exi($return);
}
if($_RQ['op']=='setgoods'){
	$return=array();
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'goodsid'=>intval($post['goodsid']),
		);
		$id=intval($post['id']);
		if(!$id){
			exi('请选择房间','error');
		}
		pdo_update('core_account_wxapp_live_rooms',$data,array('id'=>$id));
		exi('保存成功！','','goback');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_wxapp_live_rooms',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'小程序',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'choose','action'=>'jy_weishop/goods.get_goods/','name'=>'goodsid', 'help'=>'选择的商品，再详情页会显示小程序直播按钮' ),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$item=$this->plugin_setting('sale_live');
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->plugin_setting('sale_live',$post);
		exi('设置成功');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'直播小程序',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'switch','name'=>'isopen','title'=>'是否开启','help'=>'默认关闭，开启前请确认商品，小程序直播等都设置好了，否则不显示或者显示了访问直接间异常'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_wxapp_live',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'小程序',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'choose','action'=>'jy_weishop/activity.live.get_data/get_wxapps/type=all','title'=>'选择小程序','name'=>'wxapp_id', 'help'=>'要支持直播的小程序才有效' ),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array(
		'wxapp_id'=>intval($post['wxapp_id']),
	);
	$id=intval($post['id']);
	if($id){
		pdo_update('core_account_wxapp_live',$data,array('id'=>$id));
	}else{
		$data['createtime']=TIMESTAMP;
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('core_account_wxapp_live',$data);
	}
	exi('保存成功！','','goback');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_wxapp_live',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_wxapp_live',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}

